package com.freemypay.test.并发.后台线程;

import java.util.concurrent.TimeUnit;

/**
 * 后台线程判断
 * @author yesixian
 *
 */
public class Daemons {
	/**
	 * 1.若是后台线程，则它创建的线程均为后台线程
	 * 2.main()线程结束，后台线程立即终止
	 * 3.main()线程结束，非后台线程立继续
	 * @param args
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws InterruptedException {
		Thread d = new Thread(new Daemon());
		d.setDaemon(true);
		
		d.start();
		System.out.println("d.isDaemon = " + d.isDaemon() + ". ");
		TimeUnit.MICROSECONDS.sleep(2L);
	}

}

class Daemon implements Runnable {
	private Thread[] t = new Thread[1000];
	@Override
	public void run() {
		for (int j = 0; j < t.length; j++) {
			t[j] = new Thread(new DaemonSpawn());
			t[j].start();
			System.out.println("DaemonSpawn " + j + " started.");
		}
		for (int i = 0; i < t.length; i++) {
			System.out.println("t[" + i + "]，isDaemo = " + t[i].isDaemon());
		}
		while(true) {
			Thread.yield();
		}
	}
	
}

class DaemonSpawn implements Runnable {

	@Override
	public void run() {
		while(true) {
			Thread.yield();
		}
	}
	
}
